package com.sp.scmdb.action;

import java.util.List;

import net.sf.json.JSONArray;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sp.scmdb.bean.ShiftsBean;
import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.common.action.BaseAction;
import com.sp.scmdb.entity.LeaderShip;
import com.sp.scmdb.service.ShiftsService;

/**
 * 班制管理Action
 * 
 * @author Administrator
 * 
 */
@Controller("shiftsAction")
@Scope("prototype")
public class ShiftsAction extends BaseAction<ShiftsBean> {

	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(ShiftsAction.class);

	private ShiftsService shiftsService;
	private String result; 

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	@Autowired
	public void setShiftsService(ShiftsService shiftsService) {
		this.shiftsService = shiftsService;
	}

	public String findAll() {
		try {
			List<ShiftsBean> depList = shiftsService.queryAll();
			entity.setAllList(depList);
			entity.setList(depList.subList(entity.getCurrentRow(), entity
					.getLastRow()));

			this
					.button(
							"Super.Popup(5,'添加班制','/SCMDB/shiftsAction!newShifts.action','',680,300)",
							"Super.editData('/SCMDB/shiftsAction!findShifts.action?id=',680,300)",
							"Super.delData('/SCMDB/shiftsAction!delShifts.action?id=')");
		} catch (RuntimeException e) {
			log.error("findAll fail!reason:" + e.getMessage());
			return "fail";
		} catch (Exception e) {
			log.error("findAll fail!reason:" + e.getMessage());
			return "error";
		}
		return SUCCESS;
	}

	/**
	 * 跳转到班制添加页面
	 */
	public String newShifts() {

		return "newShift";
	}

	/**
	 * 添加班制
	 */
	public String addShifts() {
		try {
			UserBean user = (UserBean) session.get("user");
			String result = shiftsService.newShifts(entity, user);
			if (log.isInfoEnabled()) {
				log.info("add suncce");
			}
			messageSession(result, "班制");
		} catch (RuntimeException e) {
			log.error("addShifts fail!reason:" + e.getMessage());
			return "fail";
		} catch (Exception e) {
			log.error("addShifts fail!reason:" + e.getMessage());
			return "error";
		}
		return "commitSucc";
	}

	/**
	 * 跳转至班制修改页面
	 */
	public String findShifts() {
		try {
			String id = request.getParameter("id");
			entity = shiftsService.findID(id);
		} catch (RuntimeException e) {
			log.error("findShifts fail!reason:" + e.getMessage());
			return "fail";
		} catch (Exception e) {
			log.error("findShifts fail!reason:" + e.getMessage());
			return "error";
		}
		return "editSucc";
	}

	/**
	 * 更新班制
	 */
	public String editShifts() {
		try {
			UserBean user = (UserBean) session.get("user");
			String result = shiftsService.editShifts(entity, user);
			if (log.isInfoEnabled()) {
				log.info("edit suncce");
			}
			messageSession(result, "班制");
		} catch (RuntimeException e) {
			log.error("editShifts fail!reason:" + e.getMessage());
			return "fail";
		} catch (Exception e) {
			log.error("editShifts fail!reason:" + e.getMessage());
			return "error";
		}
		return "commitSucc";
	}

	public String delShiftsDetails() {
		try {
			String shiftsDetailID = (String) request
					.getParameter("shiftsDetailID");
			if (StringUtils.isNotEmpty(shiftsDetailID)) {
				List<LeaderShip> leaderships = shiftsService
						.queryLeadership(shiftsDetailID);
				if (CollectionUtils.isNotEmpty(leaderships)) {
					result = "此班次已经在领导排班中使用，不能删除";
				}else{
					shiftsService.delShiftsDetail(shiftsDetailID);
				}
			}
		} catch (Exception e) {
			log.error("delShiftsDetail fail!reason:" + e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 * 删除班制
	 */
	public String delShifts() {
		try {
			UserBean user = (UserBean) session.get("user");
			String depId = request.getParameter("id");
			if (StringUtils.isNotEmpty(depId)) {
				String result = shiftsService.delShifts(depId.split(","), user);
				if ("10".equals(result)) {
					messageSession("0", "该班制有所属人员，不能删除！");
					return "commitSucc";
				}
				messageSession(result, "班制");
			}
		} catch (RuntimeException e) {
			log.error("delShifts fail!reason:" + e.getMessage());
			return "fail";
		} catch (Exception e) {
			log.error("delShifts fail!reason:" + e.getMessage());
			return "error";
		}
		return "commitSucc";
	}
}
